<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\LampUser
 *
 * @property int $id
 * @property string $username
 * @property string $name
 * @property string $userpass
 * @property int $gender 性别
 * @property mixed|null $ext 扩展信息
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser query()
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser whereExt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser whereGender($value)
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser whereUsername($value)
 * @method static \Illuminate\Database\Eloquent\Builder|LampUser whereUserpass($value)
 * @mixin \Eloquent
 */
class LampUser extends Model
{
    use HasFactory;

    protected $table = 'lamp_user';


    public $timestamps = false;

    public function book()
    {
        return $this->hasMany(LampBook::class, 'uid');
    }

    public function scopeOfFilter($query, array $filters)
    {
        $query->when($filters['name'] ?? null, function ($query, $search) {
            $query->where(function ($query) use ($search) {
                $query
                    //->where('name',$search)
                    ->orWhere('name', 'like', '%' . $search . '%')
                    ->orWhere('username', 'like', '%' . $search . '%');
            });
        })->when($filters['trashed'] ?? null, function ($query, $trashed) {
            if ($trashed === 'with') {
                $query->withTrashed();
            } elseif ($trashed === 'only') {
                $query->onlyTrashed();
            }
        });
    }
}
